﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunOfAlgorithms
{
    public class Chapter_03
    {
        public void Method1()
        {
            TagKnaspsackProblem tagKnaspsackProblem = new TagKnaspsackProblem()
            {
                TagObjectsList = new List<TagObject>()
                {
                    new TagObject(){ Index = 1, Weight = 35, Price = 10,Status =0},
                    new TagObject(){ Index = 2,Weight = 30, Price = 40,Status =0},
                    new TagObject(){ Index = 3,Weight = 60, Price = 30,Status =0},
                    new TagObject(){ Index = 4,Weight = 50, Price = 50,Status =0},
                    new TagObject(){ Index = 5,Weight = 40, Price = 35,Status =0},
                    new TagObject(){ Index = 6,Weight = 10, Price = 40,Status =0},
                    new TagObject(){ Index = 7,Weight = 25, Price = 30,Status =0}
                },
                TotalC = 150
            };

            //tagKnaspsackProblem.TagObjectsList = tagKnaspsackProblem.TagObjectsList.OrderBy(t => t.Price).ToList();

            Suggestion_01(tagKnaspsackProblem, ChooseFunc1);
        }

        public void Suggestion_01(TagKnaspsackProblem tagKnaspsackProblem, Func<List<TagObject>, int, int> func)
        {
            int idx;
            int ntc = 0;

            while ((idx = func(tagKnaspsackProblem.TagObjectsList, tagKnaspsackProblem.TotalC - ntc)) != -1)
            {
                if (ntc + tagKnaspsackProblem.TagObjectsList[idx].Weight <= tagKnaspsackProblem.TotalC)
                {
                    tagKnaspsackProblem.TagObjectsList[idx].Status = 1;
                    ntc += tagKnaspsackProblem.TagObjectsList[idx].Weight;
                }
                else
                {
                    tagKnaspsackProblem.TagObjectsList[idx].Status = 2;
                }
                Console.WriteLine($"Index:{tagKnaspsackProblem.TagObjectsList[idx].Index},Weight:{tagKnaspsackProblem.TagObjectsList[idx].Weight},Price:{tagKnaspsackProblem.TagObjectsList[idx].Price},Status{tagKnaspsackProblem.TagObjectsList[idx].Status}");
            }

            Console.WriteLine();

            for (int i = 0; i < tagKnaspsackProblem.TagObjectsList.Count; i++)
            {
                Console.WriteLine($"Index:{tagKnaspsackProblem.TagObjectsList[i].Index},Weight:{tagKnaspsackProblem.TagObjectsList[i].Weight},Price:{tagKnaspsackProblem.TagObjectsList[i].Price},Status{tagKnaspsackProblem.TagObjectsList[i].Status}");
            }
            Console.WriteLine(ntc);
        }

        public int ChooseFunc1(List<TagObject> tagObjects, int c)
        {
            int _Index = -1;
            int mp = 0;
            for (int i = 0; i < tagObjects.Count; i++)
            {
                if (tagObjects[i].Status == 0 && tagObjects[i].Price > mp)
                {
                    mp = tagObjects[i].Price;
                    _Index = i;
                }
            }
            return _Index;
        }
    }


    public class TagObject
    {
        public int Index;
        public int Weight;
        public int Price;
        public int Status;
    }

    public class TagKnaspsackProblem
    {
        public int TotalC;
        public List<TagObject> TagObjectsList = new List<TagObject>();
    }
}
